package bid.yanhj.springboot.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import bid.yanhj.springboot.dao.UserRepository;
import bid.yanhj.springboot.mapper.UsersMapper;
import bid.yanhj.springboot.po.User;
import bid.yanhj.springboot.service.UsersService;

@Service
@Transactional
public class UsersServiceImpl implements UsersService {

    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private UserRepository usersRepository;
    

    public void addUserTest() {
        // TODO Auto-generated method stub
        System.out.println("insert into users.......");
    }
    @Override
    public void addUser(User user) {
        // TODO Auto-generated method stub
        this.usersMapper.insertUser(user);
    }
    @Override
    public List<User> findUserAll() {
        return this.usersMapper.selectUsersAll();
    }
    @Override
    public User findUserById(Integer userId) {
        // TODO Auto-generated method stub
        return this.usersMapper.selectUsersById(userId);
    }
    @Override
    public void updateUser(User users) {
        // TODO Auto-generated method stub
        this.usersMapper.updateUser(users);
    }
    @Override
    public void deleteUserById(Integer userId) {
        // TODO Auto-generated method stub
        this.usersMapper.deleteUserById(userId);
    }
    
    /**
     * jpa 
     * =========================================
     * =========================================
     * =========================================
     */
    @Override
    @Cacheable(value="users")
    public List<User> findUserAllRname() {
        // TODO Auto-generated method stub
        return this.usersRepository.findAll();
    }
    @Override
    //@Cacheable:对当前查询的对象做缓存处理
    @Cacheable(value="users")
    public User findUserByIdRname(Integer id) {
        // TODO Auto-generated method stub
        return this.usersRepository.findOne(id);
    }
    @Override
    @Cacheable(value="users",key="#pageable.pageSize")
    public Page<User> findUserByPageRname(Pageable pageable) {
        // TODO Auto-generated method stub
        return this.usersRepository.findAll(pageable);
    }
    // 清除缓存中以users缓存策略缓存的对象
    @Override
    @CacheEvict(value="users",allEntries=true)
    public void saveUsersRname(User user) {
        // TODO Auto-generated method stub
        this.usersRepository.save(user);
    }
}
